#include<bits/stdc++.h>
using namespace std;

#define int long long

const int N=1e6+5;

int n,m,t;
int flag,k;
int _a[N],c[N],a[N],p[N];

struct node{
    string s;
}ss[N];

inline int read(){
    int f=1,k=0;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        k=k*10+c-'0';
        c=getchar();
    }
    return f*k;
}

void A(){
    while(t--){
        int sum=m+1;
        int ans=0;
        int x[4];
        for(int i=0;i<4;i++) x[i]=read();
        for(int i=1;i<=n;i++) a[i]=_a[i]^x[i%4];
        while(sum>1){
            sum--;
            int winner[N];
            int ls=c[sum];
            int cnt=-1,tot=1;
            while(ls){
                ls/=2; cnt++;
            }
            int lls=cnt-1;
            if(cnt==1){
                ans^=sum;
                continue;
            }
            for(int i=1;i<=c[sum];i++) winner[i]=i;
            for(int i=((1<<cnt)-1);i>=1;i--){
                if((i+1)==(1<<(lls))){
                    lls--; tot=1;
                }
                //cout<<lls<<endl;
                int lz,flz;
                if(ss[cnt-lls].s[tot-1]=='1') lz=max(winner[2*tot-1],winner[2*tot]),flz=min(winner[2*tot-1],winner[2*tot]);
                else lz=min(winner[2*tot-1],winner[2*tot]),flz=max(winner[2*tot-1],winner[2*tot]);
                if(a[lz]>=cnt-lls) winner[tot++]=lz;
                else winner[tot++]=flz;
                //for(int j=1;j<tot;j++) cout<<winner[j]<<" ";
                //cout<<endl;
            }
            ans^=sum*winner[1];
        }
        cout<<ans<<endl;
    }
}

signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++) _a[i]=read();
    for(int i=1;i<=m;i++)  c[i]=read();
    while((1<<k)<n) k++;
    for(int i=1;i<=k;i++) cin>>ss[i].s;
    t=read();
    A();
    return 0;
}
